﻿// ===============================================================================
// DotNet.Platform 开发框架 2016 版权所有
// ===============================================================================

using System.Collections.Generic;
using System.Linq;
using DotNet.JCore.Entity;
using DotNet.Collections;
using DotNet.Extensions;
using DotNet.Utility;
using DotNet.Data;
using DotNet.Helper;
using System;

namespace DotNet.JCore.Service
{
    /// <summary>
    /// 检查点下班信息服务
    /// </summary>
    public class PointOffWorkService
    {
        private JCoreRepository<PointOffWork> repos = new JCoreRepository<PointOffWork>();

        /// <summary>
        /// 保存
        /// </summary>
        public BoolMessage Insert(PointOffWork entity)
        {
            repos.Insert(entity);
            return BoolMessage.True;
        }

        /// <summary>
        /// 获取对象分页集合
        /// </summary>
        /// <param name="pageCondition">分页对象</param>
        /// <param name="name">名称关键字</param>
        /// <param name="enabled">有限</param>
        /// <param name="pointId">检查点</param>
        public PageList<PointOffWork> GetPageList(PaginationCondition pageCondition,
            string pointId, DateTime? startDate, DateTime? endDate)
        {
            pageCondition.SetDefaultOrder(nameof(PointOffWork.CreateTime));
            var query = repos.PageQuery(pageCondition);
            SetQuery(query, pointId, startDate, endDate);
            return repos.Page(query);
        }

        /// <summary>
        /// 设置查询条件
        /// </summary>
        /// <param name="query">查询对象</param>
        /// <param name="search">数据对象</param>
        public void SetQuery(SQLQuery<PointOffWork> query, string pointId, DateTime? startDate, DateTime? endDate)
        {
            if (pointId.IsNotEmpty())
            {
                pointId = pointId.Trim();
                query.Where(p => p.PointId == pointId);
            }
            if (startDate.HasValue)
            {
                var startDateDt = startDate.ToDateTime();
                query.Where(p => p.CreateTime >= startDateDt);
            }

            if (endDate.HasValue)
            {
                var endDateDt = endDate.ToDateTime().AddDays(1);
                query.Where(p => p.CreateTime <= endDateDt);
            }
        }
    }
}
